/*
 * OperationPanel.java
 *
 * Created on February 15, 2008, 2:38 AM
 */

package gui.operation;

import datamodel.WSFOperation;
import javax.swing.JPanel;
import org.jdesktop.application.Action;

/**
 *
 * @author  chang
 */
public class OperationPanel extends javax.swing.JPanel {
    private WSFOperation operation;
    
    private JPanel currentPanel;
    
    /** Creates new form OperationPanel */
    public OperationPanel(WSFOperation operation) {
        this.operation = operation;
        
        initComponents();
        
        showOperation();
    }
    
    
    public void setOperation(WSFOperation operation) {
        this.operation = operation;
        showOperation();
    }
    
    private void showOperation() {
        if(!operation.isSupported()){
            showInformationPanel();
            simpleTestButton.setEnabled(false);
            makeTestCaseButton.setEnabled(false);
            return;
        }
        
        if( displayPanel.getComponentCount() == 0 ){
            showInformationPanel();
        }
        
        simpleTestButton.setEnabled(true);
        makeTestCaseButton.setEnabled(true);
        
        simpleTestButton.setEnabled(true);
        makeTestCaseButton.setEnabled(true);
        
        
        if(displayPanel.getComponent(0) == makeTestCasePanel){
            makeTestCasePanel.setOperation(operation);
            return;
        }
        
        if(displayPanel.getComponent(0) == simpleTestPanel){
            simpleTestPanel.setOperation(operation);
            return;
        }
        
        if(displayPanel.getComponent(0) == informationPanel){
            informationPanel.setOperation(operation);
            return;
        }
    }
    
    @Action
    public void showMakeTestCasePanel() {
        
        if(makeTestCasePanel == null){
            makeTestCasePanel = new MakeTestCasePanel(operation);
        }else{
            makeTestCasePanel.setOperation(operation);
        }
        
        if(makeTestCasePanel.getParent() != displayPanel){
            displayPanel.removeAll();
            displayPanel.add(makeTestCasePanel);
        }
        
        informationButton.setSelected(false);
        simpleTestButton.setSelected(false);
        makeTestCaseButton.setSelected(true);
        
        displayPanel.revalidate();
        displayPanel.repaint();
    }
    
    @Action
    public void showSimpleTestPanel(){
        if(simpleTestPanel == null){
            simpleTestPanel = new SimpleTestPanel(operation);
        }else{
            simpleTestPanel.setOperation(operation);
        }
        
        if(simpleTestPanel.getParent() != displayPanel){
            displayPanel.removeAll();
            displayPanel.add(simpleTestPanel);
        }
        
        informationButton.setSelected(false);
        simpleTestButton.setSelected(true);
        makeTestCaseButton.setSelected(false);
        
        displayPanel.revalidate();
        displayPanel.repaint();
    }
    
    @Action
    public void showInformationPanel(){
        if(informationPanel == null){
            informationPanel = new InformationPanel(operation);
        }else{
            informationPanel.setOperation(operation);
        }
        
        if(informationPanel.getParent() != displayPanel){
            displayPanel.removeAll();
            displayPanel.add(informationPanel);
        }
        
        informationButton.setSelected(true);
        simpleTestButton.setSelected(false);
        makeTestCaseButton.setSelected(false);
        
        displayPanel.revalidate();
        displayPanel.repaint();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        informationButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        simpleTestButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        makeTestCaseButton = new javax.swing.JButton();
        displayPanel = new javax.swing.JPanel();

        setName("Form"); // NOI18N

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gui.WSFApplication.class).getContext().getActionMap(OperationPanel.class, this);
        informationButton.setAction(actionMap.get("showInformationPanel")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gui.WSFApplication.class).getContext().getResourceMap(OperationPanel.class);
        informationButton.setText(resourceMap.getString("informationButton.text")); // NOI18N
        informationButton.setFocusable(false);
        informationButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        informationButton.setName("informationButton"); // NOI18N
        informationButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(informationButton);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBar1.add(jSeparator1);

        simpleTestButton.setAction(actionMap.get("showSimpleTestPanel")); // NOI18N
        simpleTestButton.setText(resourceMap.getString("simpleTestButton.text")); // NOI18N
        simpleTestButton.setFocusable(false);
        simpleTestButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        simpleTestButton.setName("simpleTestButton"); // NOI18N
        simpleTestButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(simpleTestButton);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jToolBar1.add(jSeparator2);

        makeTestCaseButton.setAction(actionMap.get("showMakeTestCasePanel")); // NOI18N
        makeTestCaseButton.setText(resourceMap.getString("makeTestCaseButton.text")); // NOI18N
        makeTestCaseButton.setFocusable(false);
        makeTestCaseButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        makeTestCaseButton.setName("makeTestCaseButton"); // NOI18N
        makeTestCaseButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(makeTestCaseButton);

        displayPanel.setName("displayPanel"); // NOI18N
        displayPanel.setLayout(new java.awt.GridLayout(0, 1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(displayPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 585, Short.MAX_VALUE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 585, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(displayPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel displayPanel;
    private javax.swing.JButton informationButton;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton makeTestCaseButton;
    private javax.swing.JButton simpleTestButton;
    // End of variables declaration//GEN-END:variables
    
    private InformationPanel informationPanel;
    private SimpleTestPanel simpleTestPanel;
    private MakeTestCasePanel makeTestCasePanel;
}
